/*
 * Copyright 2008-2009 Aying.Net with Fuchun.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.aying.ubb;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <code>FC UBB</code> 模型的配置类。
 * 
 * @author Fuchun
 * @version 1.0, 2009-6-6
 */
public class UBBConfigurer {

    private static final Log log = LogFactory.getLog(UBBConfigurer.class);

    // UBBConfigurer 的唯一实例
    private static UBBConfigurer instance = null;

    // UBBConfigurer 的实例锁
    private static final Object instanceLock = new Object();

    private static Map<String, String> ubbConfigs = new LinkedHashMap<String, String>();

    // FC UBB的默认配置信息文件路径
    private static final String DEFAULT_CONFIG_PROPERTIES = "/net/aying/ubb/fcubb.config.properties";

    // FC UBB的用户自定义的配置信息文件路径
    private static final String CUSTOM_UBBCONFIG_PROPERTIES = "/fcubb.config.properties";

    /**
     * 禁止任何外部程序直接创建 <code>UBBConfigurer</code> 实例。
     */
    private UBBConfigurer() {}

    // 初始化UBBConfigurer类的实例
    private static void initUBBConfigurerInstance() {
        if (instance == null) {
            synchronized (instanceLock) {
                if (instance == null) {
                    instance = new UBBConfigurer();
                }
            }
        }
    }

    // 初始化指定 配置文件路径的配置信息
    private static Properties getPropertiesByUBBConfigs(String configFile, boolean isLeapError)
            throws FileNotFoundException {
        initUBBConfigurerInstance();
        InputStream is = instance.getClass().getResourceAsStream(configFile);
        Properties props = new Properties();
        try {
            props.load(is);
        } catch (IOException e) {
            if (!isLeapError)
                throw new FileNotFoundException("不能读取配置文件，请确定 " + configFile + " 文件是否存在！");
        }
        return props;
    }

    // 将UBBConfig配置信息保存在Cache(Map)中
    private static void saveDefaultUBBConfigToCache() {
        // 若UBB配置信息已存在，则不会再次读取配置文件
        if(ubbConfigs.size() != 0)
            return;
        Properties props = null;
        try {
            props = getPropertiesByUBBConfigs(DEFAULT_CONFIG_PROPERTIES, false);
        } catch(FileNotFoundException e) {
            log.error(e.getMessage(), e);
        }
        if (props != null) {
            for (Iterator<? extends Object> it = props.keySet().iterator(); it.hasNext();) {
                String key = (String) it.next();
                if (ubbConfigs.containsKey(key)) {
                    String value = props.getProperty(key);
                    ubbConfigs.put(key, value == null ? "" : value);
                }
            }
        }
    }
    
    // 将用户的UBB配置信息保存在Cache(Map)中
    private static void saveCustomUBBConfigToCache() {
        Properties props = null;
        try {
            props = getPropertiesByUBBConfigs(CUSTOM_UBBCONFIG_PROPERTIES, true);
        } catch(FileNotFoundException e) {
            log.error(e.getMessage(), e);
        }
        if(props != null) {
            String sHeadingEnable = props.getProperty(UBBConstants.HEADING_ENABLE);
            boolean headingEnable = sHeadingEnable == null || "".equals(sHeadingEnable);
        }
    }

    protected static void initCustomUBBConfigs() {
        InputStream is = instance.getClass().getResourceAsStream(CUSTOM_UBBCONFIG_PROPERTIES);
        Properties customProps = new Properties();
        try {
            customProps.load(is);
        } catch (IOException e) {
            log.info("用户没有配置自定义的 " + CUSTOM_UBBCONFIG_PROPERTIES.replace("/", "") + " UBB 配置信息！");
        }
    }
}
